<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="队伍ID" prop="teamId">
        <el-input
          v-model="queryParams.teamId"
          placeholder="请输入队伍ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="队伍名称" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入队伍名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动ID" prop="actId">
        <el-input
          v-model="queryParams.actId"
          placeholder="请输入队伍所属活动ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['match:teams:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['match:teams:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['match:teams:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['match:teams:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="队伍ID" align="center" prop="teamId" />
      <el-table-column label="队伍名称" align="center" prop="teamName" />
      <el-table-column label="队伍简介" align="center" prop="teamOutline" />
      <el-table-column label="所属活动ID" align="center" prop="actId" />
      <el-table-column label="队长名称" align="center" prop="leaderName" />
      <el-table-column label="队伍状态" align="center" prop="teamStatus">
        <template #default="scope">
          <dict-tag :options="team_status" :value="scope.row.teamStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Search" @click="getTeamInfoVo(scope.row)" v-hasPermi="['match:teams:query']">查看详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['match:teams:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改队伍对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="teamsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="队伍名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入队伍名称" />
        </el-form-item>
        <el-form-item label="队伍简介" prop="teamOutline">
          <el-input v-model="form.teamOutline" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="所属活动ID" prop="actId">
          <el-input v-model="form.actId" placeholder="请输入所属活动ID" />
        </el-form-item>
        <el-form-item label="队伍状态" prop="teamStatus">
          <el-radio-group v-model="form.teamStatus">
            <el-radio
              v-for="dict in team_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>



    <el-dialog :title="队伍详情" v-model="TeamInfoOpen" width="1000px" append-to-body class="custom-dialog">
      <el-row>
        <el-col :span="12">队伍ID：{{form.teamId}}</el-col>
        <el-col :span="12">队伍名称：{{form.teamName}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="12">队长名称：{{form.leaderName}}</el-col>
        <el-col :span="24">队伍简介：{{form.teamOutline}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="12">活动ID：{{form.actId}}</el-col>
        <el-col :span="12">活动名称：{{form.actTitle}}</el-col>
      </el-row>
      <!-- 队员信息 -->
      <el-row>
        <el-col :span="24"><b>队员名单：</b></el-col>
      </el-row>
      <el-row v-for="(row, index) in formattedMembers" :key="index">
        <el-col :span="12" v-for="(member, idx) in row" :key="idx">
          {{ member }}
        </el-col>
      </el-row>
    </el-dialog>


  </div>
</template>

<style scoped>
  .custom-dialog {
    border-radius: 12px;
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
    background-color: #fff;
    width: 600px; /* 增大对话框宽度 */
  }

  .custom-dialog .el-dialog__header {
    background-color: #f0f9eb;
    border-bottom: 1px solid #ebedf0;
    padding: 20px; /* 增加头部的内边距 */
  }

  .custom-dialog .el-dialog__title {
    color: #67c23a;
    font-size: 22px; /* 增大标题字体大小 */
    font-weight: bold; /* 标题加粗 */
  }

  .custom-dialog .el-row {
    margin-bottom: 20px; /* 增大行之间的间距 */
  }

  .custom-dialog .el-col {
    color: #333;
    font-size: 16px; /* 增大内容字体大小 */
  }

  .custom-dialog .el-col b {
    color: #606266;
    font-size: 18px; /* 增大加粗文本的字体 */
  }

  .custom-dialog .el-col .el-row {
    margin-bottom: 10px;
  }

</style>


<script setup name="Teams">
import { listTeams, getTeams, delTeams, addTeams, updateTeams } from "@/api/match/teams";

const { proxy } = getCurrentInstance();
const { team_status } = proxy.useDict('team_status');

const teamsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    teamId: null,
    teamName: null,
    actId: null,
  },
  rules: {
    teamName: [
      { required: true, message: "队伍名称不能为空", trigger: "blur" }
    ],
    teamOutline: [
      { required: true, message: "队伍简介不能为空", trigger: "blur" }
    ],
    actId: [
      { required: true, message: "所属活动ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询队伍列表 */
function getList() {
  loading.value = true;
  listTeams(queryParams.value).then(response => {
    teamsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    teamId: null,
    teamName: null,
    teamOutline: null,
    actId: null,
    teamStatus: null
  };
  proxy.resetForm("teamsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.teamId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加队伍";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _teamId = row.teamId || ids.value
  getTeams(_teamId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改队伍";
  });
}

/** 查看详情按钮操作 */
const TeamInfoOpen = ref(false)
function getTeamInfoVo(row) {
  reset();
  const _teamId = row.teamId
  getTeams(_teamId).then(response => {
    form.value = response.data;
    TeamInfoOpen.value = true;
  });
}





/** 提交按钮 */
function submitForm() {
  proxy.$refs["teamsRef"].validate(valid => {
    if (valid) {
      if (form.value.teamId != null) {
        updateTeams(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTeams(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _teamIds = row.teamId || ids.value;
  proxy.$modal.confirm('是否确认删除队伍编号为"' + _teamIds + '"的数据项？').then(function() {
    return delTeams(_teamIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('match/teams/export', {
    ...queryParams.value
  }, `teams_${new Date().getTime()}.xlsx`)
}

getList();


import { computed } from "vue";

const formattedMembers = computed(() => {
  if (!form.value.memberNames) return [];
  const members = form.value.memberNames.split(", "); // 按逗号分割
  const result = [];
  for (let i = 0; i < members.length; i += 2) {
    result.push(members.slice(i, i + 2)); // 每两个一组
  }
  return result;
});

</script>
